package com.ruoyi.utils.txy;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.SSL.domain.SslApply;
import com.ruoyi.common.core.domain.AjaxResult;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.teo.v20220901.TeoClient;
import com.tencentcloudapi.teo.v20220901.models.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TxyEdgeOneDomainUtils {

    public static TeoClient createClient(String secretId, String secretKey){
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("teo.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        return new TeoClient(cred, "", clientProfile);
    }

    public static AjaxResult listDescribeAccelerationDomains(String secretId, String secretKey){
        try {
            TeoClient client = createClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeZonesRequest req = new DescribeZonesRequest();
            // 返回的resp是一个DescribeZonesResponse的实例，与请求对象对应
            DescribeZonesResponse resp = client.DescribeZones(req);
            List<AccelerationDomain> accelerationDomainList = new ArrayList<>();
            for (Zone zone : resp.getZones()) {
                // 实例化一个请求对象,每个接口都会对应一个request对象
                DescribeAccelerationDomainsRequest req1 = new DescribeAccelerationDomainsRequest();
                req1.setZoneId(zone.getZoneId());
                // 返回的resp是一个DescribeAccelerationDomainsResponse的实例，与请求对象对应
                DescribeAccelerationDomainsResponse resp1 = client.DescribeAccelerationDomains(req1);
                accelerationDomainList.addAll(Arrays.asList(resp1.getAccelerationDomains()));
            }
            return AjaxResult.success("查询腾讯云EdgeOne域名成功！", accelerationDomainList);
        } catch (Exception e) {
            return AjaxResult.error("查询腾讯云EdgeOne域名错误："+e.getMessage());
        }
    }

    public static AjaxResult putDescribeAccelerationDomainCert(String secretId, String secretKey, String zoneId, SslApply sslApply, String domainTxt, String ptName){
        try {
            AjaxResult aj = TxySSLUtils.upLoadDescribeCertificates(secretId, secretKey, sslApply, ptName);
            if(aj.isSuccess()){
                //获得请求客户端对象
                TeoClient client = createClient(secretId, secretKey);
                // 实例化一个请求对象,每个接口都会对应一个request对象
                ModifyHostsCertificateRequest req = new ModifyHostsCertificateRequest();
                req.setZoneId(zoneId);
                req.setHosts(new String[]{domainTxt});
                ServerCertInfo serverCertInfo = new ServerCertInfo();
                serverCertInfo.setCertId(String.valueOf(aj.get("data")));
                req.setServerCertInfo(new ServerCertInfo[]{serverCertInfo});
                // 返回的resp是一个ModifyHostsCertificateResponse的实例，与请求对象对应
                ModifyHostsCertificateResponse resp = client.ModifyHostsCertificate(req);
                Console.log(JSONUtil.toJsonStr(resp));
                return AjaxResult.success("更新腾讯云EdgeOne域名证书成功！");
            }
            return aj;
        } catch (Exception e) {
            return AjaxResult.error("更新腾讯云EdgeOne域名证书错误："+e.getMessage());
        }
    }

    public static void main(String[] args) {
        try {
            //获得请求客户端对象
            TeoClient client = createClient("AKIDrrlmfaNFJYiu2zmUUSHLFzXszymMiAbJ", "glhQwEJH3iDar1zymNlv7CF9qJ6nKgom");
            // 实例化一个请求对象,每个接口都会对应一个request对象
            ModifyHostsCertificateRequest req = new ModifyHostsCertificateRequest();
            req.setZoneId("zone-36605xxb9oki");
            req.setHosts(new String[]{"txy-edgeone.yygzs.org"});
            ServerCertInfo serverCertInfo = new ServerCertInfo();
            serverCertInfo.setCertId("KgIV0aaz");
            req.setServerCertInfo(new ServerCertInfo[]{serverCertInfo});
            // 返回的resp是一个ModifyHostsCertificateResponse的实例，与请求对象对应
            ModifyHostsCertificateResponse resp = client.ModifyHostsCertificate(req);
            Console.log(JSONUtil.toJsonStr(resp));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
